package com.ruiyian.project.tunnel.controller;

import com.google.common.collect.Maps;
import com.ruiyian.common.core.controller.BaseController;
import com.ruiyian.common.core.domain.AjaxResult;
import com.ruiyian.common.core.domain.entity.SysDictData;
import com.ruiyian.common.core.page.TableDataInfo;
import com.ruiyian.project.equipment.service.EquipmentService;
import com.ruiyian.project.tunnel.domain.Hole;
import com.ruiyian.project.tunnel.domain.Tunnel;
import com.ruiyian.project.tunnel.service.TunnelService;
import com.ruiyian.system.service.ISysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2021/1/20
 * Desc: 隧道控制层
 */
@Slf4j
@RestController
@RequestMapping("/tunnel/info")
public class TunnelController extends BaseController {
    @Autowired
    private TunnelService tunnelService;
    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 隧道信息列表
     * @param tunnel
     * @return
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Tunnel tunnel) {
        startPage();
        List<Tunnel> list = tunnelService.selectTunnelList(tunnel);
        return getDataTable(list);
    }


    /**
     * 隧道列表
     * @param tunnel
     * @return
     */
    @PostMapping("/getTunnelList")
    public AjaxResult getTunnelList(@RequestBody Tunnel tunnel) {
        List<Tunnel> list = tunnelService.selectTunnelList(tunnel);
        return AjaxResult.success(list);
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Long id) {
        try {
            tunnelService.delete(id);
        } catch (Exception e) {
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 保存
     * @param tunnel
     * @return
     */
    @PutMapping("/save")
    public AjaxResult save(@RequestBody Tunnel tunnel) {
        try {
            if(tunnel.getId()!=null){
                tunnelService.update(tunnel);
            }else {
                tunnelService.saveAll(tunnel);
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 根据id获取对象
     * @param id
     * @return
     */
    @PostMapping("/selectTunnelById")
    public AjaxResult selectTunnelById(@RequestBody Long id) {
        Tunnel tunnel = null;
        try {
            tunnel = tunnelService.selectTunnelById(id);
        } catch (Exception e) {
           return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(tunnel);
    }

    /**
     * 根据隧道id获取洞
     * @param id
     * @return
     */
    @PostMapping("/selectHoleById")
    public AjaxResult selectHoleById(@RequestBody Long id) {
        List<Hole> holeList = null;
        try {
            holeList = tunnelService.selectHoleById(id);
        } catch (Exception e) {
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(holeList);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/deleteHole")
    public AjaxResult deleteHole(@RequestBody Long id) {
        try {
            tunnelService.deleteHole(id);
        } catch (Exception e) {
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('tunnel:remove')")
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(@RequestBody List<Tunnel> id) {
        try {
            tunnelService.deleteBatch(id);
        } catch (Exception e) {
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 更新标题
     * @param tunnel
     * @return
     */
    @PostMapping("/updateTitle")
    public AjaxResult updateTitle(@RequestBody Tunnel tunnel) {
        try {
            tunnelService.updateTitle(tunnel);
        } catch (Exception e) {
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 根据隧道id获取模块列表
     * @param id
     * @return
     */
    @PostMapping("/selectListByTunnelId")
    public AjaxResult selectListByTunnelId(@RequestBody Long id) {
        Map<String,Object> map = Maps.newHashMap();
        List<SysDictData> sysDictData = null;
        try {
            Tunnel tunnel = tunnelService.selectTunnelById(id);
            sysDictData = dictDataService.selectListByTunnelId(tunnel.getModuleInfo());
            map.put("tunnel",tunnel);
            map.put("sysDictData",sysDictData);
        } catch (Exception e) {
           return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(map);
    }
}
